package com.piggy.common.core.web.page;

import cn.hutool.core.util.StrUtil;
import com.piggy.common.core.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 排序元素载体
 *
 * @author HCL
 * Create at 2019/5/27
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderByItem implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 需要进行排序的字段
     */
    private String column;
    /**
     * 是否正序排列，默认 true
     */
    private boolean asc = true;

    public final static String C_ASC = "asc";
    public final static String C_DESC = "desc";

    public static OrderByItem asc(String column) {
        return build(column, true);
    }

    public static OrderByItem desc(String column) {
        return build(column, false);
    }

    public static List<OrderByItem> ascs(String... columns) {
        return Arrays.stream(columns).map(OrderByItem::asc).collect(Collectors.toList());
    }

    public static List<OrderByItem> descs(String... columns) {
        return Arrays.stream(columns).map(OrderByItem::desc).collect(Collectors.toList());
    }

    private static OrderByItem build(String column, boolean asc) {
        return new OrderByItem(column, asc);
    }

    public String toString() {
        return StringUtils.toUnderScoreCase(getColumn()) + StrUtil.C_SPACE + (asc ? C_ASC : C_DESC);
    }

}
